/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2020-06-09 10:10:38
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-06-15 16:11:01
 */
// pages/question_answer_card/question_answer_card.js
import urlconf from '../../utils/urlconf.js'
import httpRequest from '../../utils/request.js'
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        answerSheetList: [],
        examId: '',
        countNum: 0,
        name: ''
    },

    /**
     * 点击答题卡的某一项
     */
    onClickCardItem: function (e) {
        console.log(e);
        // let pages = getCurrentPages();
        // let prevPage = pages[pages.length - 2];
        let index = e.currentTarget.dataset.index;
        console.log(index);
        wx.setStorageSync('historyIndex', index)
        // prevPage.setData({
        //     tabSelectedIndex: index,
        // })
        wx.navigateBack({
            delta: 1,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        let { examId, countNum, name } = options
        this.setData({
            examId,
            countNum,
            name
        })

    },
    submit() { //提交试卷
        let examId = Number(this.data.examId);
        let countNum = this.data.countNum
        wx.showLoading({
            title: '计算分数中'
        });
        httpRequest(urlconf.commit, { examId }, 'POST').then(res => {
            console.log('提交试卷result', res);
            wx.hideLoading();
            if (res.code == 1) { //提交成功
                wx.showToast({
                    title: '提交成功',
                    icon: 'none'
                })
                let timer = setTimeout(() => {
                    wx.navigateTo({
                        url: `../examResult/examResult?examId=${examId}&&nums=${countNum}&&lastPage=card&&name=${this.data.name}`
                    })
                    clearTimeout(timer);
                }, 700)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                })
                let timer = setTimeout(() => {
                    wx.navigateBack({
                        delta: 2
                    });
                    clearTimeout(timer);
                }, 1000)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let { examId, countNum } = this.data
        let answerSheetList = []
        for (var i = 0; i < countNum; i++) {
            answerSheetList.push({
                num: i + 1,
                isDone: false
            })
        }
        console.log(answerSheetList);
        this.setData({
            answerSheetList
        })
        httpRequest(urlconf.beforeCommit, { examId }, 'GET').then(res => {
            if (res.code == 1) {
                answerSheetList.map((item, i) => {
                    for (let j in res.data) {
                        if (item.num == res.data[j]) {
                            item.isDone = true
                        }
                    }
                })
                this.setData({
                    answerSheetList
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // 销毁时恢复上一页的切换动画
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            swiperDuration: "250",
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let shareImgUrl = app.globalData.shareImg
        return {
            imageUrl: shareImgUrl,
            path: '/pages/index/index'
        };
    }
})